// 
//

const GameStatus = require("GameStatus");
const SoundType = require("SoundType");
const AudioSourceControl = require("AudioSourceControl");


let MainControl =  cc.Class({
    extends: cc.Component,
    properties: {

        label:{
            default:null,
            type:cc.Label,
        },
        
        spBg:{
            default:[],
            type:[cc.Sprite]
        },

        pipePrefab:cc.Prefab,
        pipe:[cc.Node],

        spGameOver:{
            default:null,
            type:cc.Sprite
        },

        btnStart:{
            type:cc.Button,
            default:null,
        },

        audioControl:{
            type:AudioSourceControl,
            default:null,
        },

        gameStatus:GameStatus.Game_Ready,

        gameScore:0,

    },

    
    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        console.log(GameStatus);
        console.log("gameStatus" ,GameStatus.Game_Playing,GameStatus.Game_Ready,GameStatus.Game_Over);
        let collisionManager = cc.director.getCollisionManager();
        collisionManager.enabled = true;
        // collisionManager.enabledDebugDraw = true;

        this.spGameOver = this.node.getChildByName('GameOver').getComponent(cc.Sprite);
        this.spGameOver.node.active = false;

        this.btnStart = this.node.getChildByName('BtnStart').getComponent(cc.Button);
        this.btnStart.node.on(cc.Node.EventType.TOUCH_END,this.touchStartBtn,this);

        this.audioControl = this.node.getChildByName("AudioSource").getComponent("AudioSourceControl");
    },

    start () {

        for(let i = 0;i < 3;++i){

            this.pipe[i] = cc.instantiate(this.pipePrefab);
            this.node.getChildByName('Pipe').addChild(this.pipe[i]);
            this.pipe[i].x = 170 + 200 *i;
            let minY = -120;
            let maxY = 120;
            this.pipe[i].y = minY + Math.random()*(maxY - minY);
        }
    },

    update (dt) {

        if(this.gameStatus != GameStatus.Game_Playing)
            return;

        for(let i = 0;i < 2; ++i){
            this.spBg[i].node.x -= 1.0;
            if(this.spBg[i].node.x <= -288){
                this.spBg[i].node.x = 288;
            }
        }

        // 移动障碍物
        for(let i = 0;i <3;++i){
            this.pipe[i].x -=1.0;
            if(this.pipe[i].x <= -170){
                this.pipe[i].x = 430;

                let minY = -120;
                let maxY = 120;
                this.pipe[i].y = minY + Math.random() *(maxY - minY);

                this.gameScore++;
                this.label.string = this.gameScore.toString();
                this.audioControl.playSound(SoundType.E_SOUND_SCORE);
            }
        }
    },

    gameOver(){
        this.spGameOver.node.active = true;
        this.btnStart.node.active = true;
        this.gameStatus = GameStatus.Game_Over;
        this.audioControl.playSound(SoundType.E_SOUND_DIE);
        console.log("gameStatus -> ",this.gameStatus);

    },

    touchStartBtn(event){

        this.btnStart.node.active = false;
        this.gameStatus = GameStatus.Game_Playing;
        this.spGameOver.node.active = false;
        for(let i = 0;i <3;i++){

            this.pipe[i].x = 170 + 200 *i;
            let minY = -120;
            let maxY = 120;
            this.pipe[i].y = minY + Math.random()*(maxY-minY);
        }
        let bird = this.node.getChildByName("Bird");
        bird.y = 0;
        bird.rotation - 0;

        this.gameScore = 0;
        this.label.string = this.gameScore.toString();
    }
});

module.exports = MainControl;